package com.wsjc.manager.service.rpc.data.impl;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.Maps;
import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.common.core.dao.db.TableRoute;
import com.wsjc.common.solr.SolrClientUtils;
import com.wsjc.manager.dao.hospitaladdtional.WjHospitalAdditionalDao;
import com.wsjc.manager.dao.sterilization.WjSterilizationDao;
import com.wsjc.manager.entity.WjHospitalAdditional;
import com.wsjc.manager.entity.WjSterilization;
import com.wsjc.manager.service.rpc.data.WjHospitalDataService;

@Service("wjHospitalDataService")
public class WjHospitalDataServiceImpl implements WjHospitalDataService{

	private static Logger logger = LoggerFactory.getLogger(WjHospitalDataServiceImpl.class);
	
	
	@Autowired
	private SolrClientUtils solrClientUtils ;
	
	@Autowired
	private WjSterilizationDao wjSterilizationDaoImpl;
	
	@Autowired
	private WjHospitalAdditionalDao wjHospitalAddtionalDaoImpl;
	
	
	@SuppressWarnings("unchecked")
	public void flushData(String hospitalId, String dataType, String dataId){

		String areaId = hospitalId.substring(3, 9);
		Map<String, Object> paramMap = Maps.newHashMap();
		switch (dataType) {
			case "00S":
				paramMap.put("tableName", CommonConst.WJ_STERILIZATION) ;
				paramMap.put("id", dataId) ;
				TableRoute.setTableName(paramMap, areaId);
				List<WjSterilization> sterilizations = wjSterilizationDaoImpl.listBy(paramMap) ;
				//把文档对象写入索引库
				solrClientUtils.addBeans(sterilizations);
				/** 添加医院详情信息 **/
				addHospitalAddtionalDao(sterilizations, areaId);
				logger.info("同步Sterilization数据到Solr: Successful...");
				break;
			default:
				break;
		}
		
	}


	/**
	 * 补充医院详情信息
	 * @param sterilizations
	 */
	private void addHospitalAddtionalDao(List<WjSterilization> sterilizations, String areaId) {
		
		WjSterilization sterilization = sterilizations.get(0);
		
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("deviceBrand", sterilization.getDeviceBrand());
		paramMap.put("opDepartment", sterilization.getOpDepartment());
		long num = wjHospitalAddtionalDaoImpl.getCountByColumn(paramMap);
		if(num > 0){
			return ;
		}
		WjHospitalAdditional additional = new WjHospitalAdditional();
		additional.setId(areaId+System.currentTimeMillis());
		additional.setOpDepartment(sterilization.getOpDepartment());
		additional.setDeviceBrand(sterilization.getDeviceBrand());
		additional.setDeviceCapacity(sterilization.getDeviceCapacity());
		additional.setDeviceKind(sterilization.getDeviceKinds());
		additional.setDeviceLevel(sterilization.getDeviceLevel());
		additional.setDeviceProgramNumber(sterilization.getDeviceProgramNumber());
		additional.setDeviceType(sterilization.getDeviceType());
		additional.setHospitalId(sterilization.getHospitalId());
		wjHospitalAddtionalDaoImpl.insert(additional);
		
	}


	@Override
	public void addDatasToSolr(List<Object> object) {
		
		try {
			long startTime = System.currentTimeMillis();
			//把文档对象写入索引库
			solrClientUtils.addBeans(object); 
			logger.info("同步Data数据到Solr: Successful|一共耗时:{}秒", (double) (System.currentTimeMillis() - startTime) / 1000);
		} catch (Exception e) {
			logger.error("同步Sterilization数据到Solr: 失败|info:", e);
		}
		
	}


	@Override
	public void addSterilizationDataToDB(WjSterilization wjSterilization) {
		
		String areaId = wjSterilization.getHospitalId().substring(3, 9);
		String tableName = TableRoute.getTableName(CommonConst.DATA_TABLE_NAME_STERILIZATION, areaId);
		wjSterilization.setTableName(tableName);
		wjSterilizationDaoImpl.insert(wjSterilization);
		
	}


	@Override
	public void deleteSterilizationData(WjSterilization wjSterilization) {
		
		String areaId = wjSterilization.getHospitalId().substring(3, 9);
		Map<String, Object> paramMap = Maps.newHashMap();
		String tableName = TableRoute.getTableName(CommonConst.DATA_TABLE_NAME_STERILIZATION, areaId);
		paramMap.put("tableName", tableName);
		paramMap.put("id", wjSterilization.getId());
		wjSterilizationDaoImpl.delete(paramMap);
		
	}
	
	
	
	
	
}
